package com.gy.spring.mvc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gy.spring.mvc.service.RedisService;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String value = null;
        try {
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedis);
        }
        return value;
    }

    @Override
    public void set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedis);
        }
    }

    @Override
    public List<String> getList(String key, long start, long end) {
        Jedis jedis = jedisPool.getResource();
        List<String> list = null;
        try {
            list = jedis.lrange(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedis);
        }
        return list;
    }

    @Override
    public void set(String key, List<String> value) {
        Jedis jedis = jedisPool.getResource();
        try {

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedis);
        }
    }

    @Override
    public void del(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedis);
        }
    }


    /**
     * TODO 释放资源
     *
     * @param jedis
     * @author geYang
     * @date 2018-04-10 10:13
     */
    private void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

}
